package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.*;
import com.cskaoyan.app.mapper.*;
import com.cskaoyan.app.service.AdminUserService;
import com.cskaoyan.app.util.StringsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private MarketUserMapper userMapper;

    @Autowired
    private MarketAddressMapper addressMapper;

    @Autowired
    private MarketCollectMapper collectMapper;

    @Autowired
    private MarketFootprintMapper footprintMapper;

    @Autowired
    private MarketSearchHistoryMapper historyMapper;

    @Autowired
    private MarketFeedbackMapper feedbackMapper;

    @Override
    public CommonData<MarketUser> listUsers(BasePageInfo pageParam, String username, String mobile) {
        Integer page = pageParam.getPage();
        Integer limit = pageParam.getLimit();
        String sort = pageParam.getSort();
        String order = pageParam.getOrder();
        MarketUserExample userExample = new MarketUserExample();
        userExample.setOrderByClause(StringsUtil.getOrderClause(sort, order));
        MarketUserExample.Criteria criteria = userExample.createCriteria();
        if (!StringsUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringsUtil.isEmpty(mobile)) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketUser> marketUsers = userMapper.selectByExample(userExample);
        PageInfo<MarketUser> pageInfo = new PageInfo<>(marketUsers);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketUser getUserDetailById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public CommonData<MarketAddress> listAddr(BasePageInfo pageParam, String name, Integer userId) {
        Integer page = pageParam.getPage();
        Integer limit = pageParam.getLimit();
        String sort = pageParam.getSort();
        String order = pageParam.getOrder();

        MarketAddressExample addressExample = new MarketAddressExample();
        addressExample.setOrderByClause(StringsUtil.getOrderClause(sort, order));
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringsUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketAddress> addresses = addressMapper.selectByExample(addressExample);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(addresses);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketCollect> listCollect(BasePageInfo pageParam, Integer userId, Integer valueId) {
        Integer page = pageParam.getPage();
        Integer limit = pageParam.getLimit();
        String sort = pageParam.getSort();
        String order = pageParam.getOrder();

        MarketCollectExample collectExample = new MarketCollectExample();
        collectExample.setOrderByClause(StringsUtil.getOrderClause(sort, order));
        MarketCollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketCollect> collects = collectMapper.selectByExample(collectExample);
        PageInfo<MarketCollect> pageInfo = new PageInfo<>(collects);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketFootprint> listFootPrint(BasePageInfo pageParam, Integer userId, Integer goodsId) {
        Integer page = pageParam.getPage();
        Integer limit = pageParam.getLimit();
        String sort = pageParam.getSort();
        String order = pageParam.getOrder();

        MarketFootprintExample footprintExample = new MarketFootprintExample();
        footprintExample.setOrderByClause(StringsUtil.getOrderClause(sort, order));
        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketFootprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo<MarketFootprint> pageInfo = new PageInfo<>(footprints);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketSearchHistory> listHistory(BasePageInfo pageParam, Integer userId, String keyword) {
        Integer page = pageParam.getPage();
        Integer limit = pageParam.getLimit();
        String sort = pageParam.getSort();
        String order = pageParam.getOrder();

        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        historyExample.setOrderByClause(StringsUtil.getOrderClause(sort, order));

        MarketSearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringsUtil.isEmpty(keyword)) {
            criteria.andKeywordLike("%"+keyword+"%");
        }
        PageHelper.startPage(page, limit);
        criteria.andDeletedEqualTo(false);
        List<MarketSearchHistory> histories = historyMapper.selectByExample(historyExample);
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(histories);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketFeedback> listFeedback(BasePageInfo pageParam, Integer id, String username) {
        Integer page = pageParam.getPage();
        Integer limit = pageParam.getLimit();
        String sort = pageParam.getSort();
        String order = pageParam.getOrder();

        MarketFeedbackExample feedbackExample = new MarketFeedbackExample();
        feedbackExample.setOrderByClause(StringsUtil.getOrderClause(sort, order));

        MarketFeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (!StringsUtil.isEmpty(username)) {
            criteria.andUsernameLike(username);
        }
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketFeedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo<MarketFeedback> pageInfo = new PageInfo<>(feedbacks);
        return CommonData.data(pageInfo);
    }
}
